<template>
  <div class="app-container">
    <!-- 条件栏 -->
    <div class="filter-wrapper">
      <NewPageFilter
        v-model:query="filterInfo.query"
        :filter-list="filterInfo.list"
        :filter-btn="filterInfo.btn"
      >
        <template v-slot:filter-operateTime>
          <el-date-picker
            v-model="operateTime"
            format="YYYY-MM-DD"
            value-format="YYYY-MM-DD"
            type="daterange"
            range-separator="至"
            :clearable="true"
            :picker-options="$fn.pickerOptionsFn()"
            align="right"
            start-placeholder="操作时间开始日期"
            end-placeholder="操作时间结束日期"
            :shortcuts="$fn.pickerOptionsFn()"
            :default-time="[
              new Date(2000, 1, 1, 0, 0, 0),
              new Date(2000, 1, 1, 23, 59, 59),
            ]"
            @change="timeChange"
          ></el-date-picker>
        </template>
      </NewPageFilter>
    </div>
    <div class="flex-line">
      <NewPageFilter :filter-btn="filterInfo.btnList"> </NewPageFilter>
      &emsp;
      <PopoverPrint
        ref="popoverShow"
        v-model:popoverVisible="popoverVisible"
        :canPrint="$permission('maintenanceStatusSummary:print')"
        :templateArr="templateArr"
        @tempClick="tempClick"
        @print="print"
      ></PopoverPrint>
    </div>
    <!-- 表格 -->
    <TableContainer
      :refresh="tableInfo.refresh"
      :init-curpage="tableInfo.initCurpage"
      :pageTotal="pageInfos.pageTotal"
      :api="api"
      v-model:data="tableInfo.data"
      v-model:query="filterInfo.query"
      :tab-index="false"
      :field-list="tableInfo.fieldList"
      :handle="tableInfo.handle"
      :summary="true"
      :summary-field-list="tableInfo.summaryFieldList"
      :pager="true"
      :checkBox="false"
    />
  </div>
</template>

<script setup name="maintenanceStatusSummary">
import {
  maintenanceStatusSummaryList,
  exportMaintenanceSummaryExcel,
  // printCostStatistics,
} from '@/api/dataQuery'
import { doPrint } from '@/utils/exportUtil.js'
import { selectByCodeAndDept } from '@/api/report.js'
import { getDeptAll } from '@/api/core'
import moment from 'moment'
const { proxy } = getCurrentInstance()

const operateTime = ref(['', ''])
const popoverVisible = ref(false)
const templateArr = ref([])
const pageInfos = ref({
  pageSize: 20,
  curPage: 1,
  pageTotal: 0,
})
const api = ref('')
// 过滤相关配置
const filterInfo = ref({
  query: {
    startDate: '',
    endDate: '',
    // useDeptId: "",
  },
  copyQuery: {},
  list: [
    {
      slot: true,
      label: '起止日期',
      value: 'operateTime',
    },
    // {
    //   componentType: "slip-select",
    //   label: "部门",
    //   value: "useDeptId",
    //   multiple: true,
    //   api: getDeptAll,
    //   query: { deptAuth: "id", status: "1", useTypeId: "1" },
    //   event: search,
    // },
  ],
  btn: [
    { type: 'button', label: '搜索', event: search },
    { type: 'button', label: '重置', event: clearSearch },
  ],
  btnList: [
    {
      type: 'button',
      label: '导出excel',
      event: exportExcel,
      show: proxy.$permission('maintenanceStatusSummary:excel'),
    },
    // {
    //   type: "button",
    //   label: "打印",
    //   event: print,
    //   show: proxy.$permission("maintenanceStatusSummary:print"),
    // },
  ],
})
// 表格相关
const tableInfo = ref({
  refresh: 1,
  initCurpage: 1,
  data: [],
  fieldList: [
    { label: '部门', value: 'maintenanceName' },
    { label: '待接单', value: 'pendAcceptCount' },
    { label: '进行中', value: 'processingCount' },
    { label: '已完工', value: 'dealCompleteCount' },
    { label: '已完成', value: 'completeCount' },
    { label: '已作废', value: 'invalidCount' },
    { label: '已撤销', value: 'revokeCount' },
    { label: '合计', value: 'totalCount' },
  ],
  summaryFieldList: [
    { value: 'pendAcceptCount', formatter: proxy.$fn.TripartiteMethod },
    { value: 'processingCount', formatter: proxy.$fn.TripartiteMethod },
    { value: 'dealCompleteCount', formatter: proxy.$fn.TripartiteMethod },
    { value: 'completeCount', formatter: proxy.$fn.TripartiteMethod },
    { value: 'invalidCount', formatter: proxy.$fn.TripartiteMethod },
    { value: 'revokeCount', formatter: proxy.$fn.TripartiteMethod },
    { value: 'totalCount', formatter: proxy.$fn.TripartiteMethod },
  ],
})

onMounted(() => {
  filterInfo.value.copyQuery = proxy.$fn.deepClone(filterInfo.value.query)

  // gettime()
  api.value = maintenanceStatusSummaryList
  getList()
})

function gettime() {
  const now = new Date()
  const nowYear = now.getFullYear()
  const nowMonth = now.getMonth()
  const first = moment(new Date(nowYear, nowMonth, 1, 0, 0)).format(
    'YYYY-MM-DD'
  )
  const second = moment().format('YYYY-MM-DD')

  filterInfo.value.query.startDate = first
  filterInfo.value.query.endDate = second
  operateTime.value[0] = first
  operateTime.value[1] = second
}

function exportExcelList() {
  let params = {
    ...filterInfo.value.query,
    curPage: pageInfos.value.curPage,
    pageSize: pageInfos.value.pageSize,
  }
  exportMaintenanceSummaryExcel(params).then((res) => {
    if (res) {
      proxy.$fn.parseExcel(res)
    }
  })
}

// 获取列表
function getList() {
  tableInfo.value.refresh = Math.random()
}
function clearSearch() {
  // filterInfo.value.query.useDeptId = ''
  // gettime()
  filterInfo.value.query.startDate = ''
  filterInfo.value.query.endDate = ''
  operateTime.value[0] = ''
  operateTime.value[1] = ''
  getList()
}
//打印
function print() {
  selectByCodeAndDept({
    tempCode: 'maintenanceStatusSummary',
  }).then((res) => {
    if (res && res.success) {
      templateArr.value = res.data
      popoverVisible.value = true
      // 一条数据则直接打印
      if (templateArr.value.length === 1) {
        console.log(proxy.$refs.popoverShow)
        proxy.$refs.popoverShow.hidePopover()
        doPrint({
          name: templateArr.value[0].ureportName,
          params: {
            ...filterInfo.value.query,
          },
          callback: printCallback,
          data: {},
        })
      }
    }
  })
}
function tempClick(item) {
  doPrint({
    name: item.ureportName,
    params: {
      ...filterInfo.value.query,
    },
    callback: printCallback,
    data: {},
  })
  popoverVisible.value = false
}

function printCallback() {
  // try {
  //   // 打印成功增加打印次数
  //   updatePrintCount({
  //     orderNo: infoList.value.data.orderNo,
  //     orderId: props.orderId,
  //     orderType: 0,
  //   })
  // } catch (error) {}
}

// 搜索
function search() {
  getList()
}
function timeChange(val) {
  if (val && val.length) {
    filterInfo.value.query.startDate = val[0]
    filterInfo.value.query.endDate = val[1]
  } else {
    filterInfo.value.query.startDate = ''
    filterInfo.value.query.endDate = ''
  }
  search()
}
function exportExcel() {
  exportExcelList()
}
</script>

<style scoped lang="scss">
.flex-line {
  display: flex;
  flex-direction: row;
}
</style>
